package home

import (
	"Web-Shop/controllers/goods"
	"Web-Shop/models"
	"log"
)

func (c *HomeController) ShowHome() {
	c.TplName = "index.html"

	goodsTypes := make([]*models.GoodsType, 0)
	indexGoodsBanners := make([]*models.IndexGoodsBanner, 0)
	indexPromotionBanners := make([]*models.IndexPromotionBanner, 0)
	indexGoodsCates := make([]map[string]interface{}, len(goodsTypes))

	indexGoodsBanners, err := (&models.IndexGoodsBanner{}).ReadForHome()
	goodsTypes = goods.GoodsTypes(&c.BaseController)
	if err != nil {
		log.Println(err)
	}

	indexPromotionBanners, err = (&models.IndexPromotionBanner{}).ReadForHome()
	if err != nil {
		log.Println(err)
	}

	for _, v := range goodsTypes {
		temp := make(map[string]interface{})
		temp["type"] = v
		indexGoodsCates = append(indexGoodsCates, temp)
	}

	for _, v := range indexGoodsCates {
		textGoods := []*models.IndexTypeGoodsBanner{}
		imgGoods := []*models.IndexTypeGoodsBanner{}

		if textGoods, imgGoods, err = (&models.IndexTypeGoodsBanner{}).ReadGoodsCate(v["type"], textGoods, imgGoods); err != nil {
			log.Println(err)
			return
		}

		v["textGoods"] = textGoods
		v["imgGoods"] = imgGoods
	}

	c.Data["indexGoodsBanners"] = indexGoodsBanners
	c.Data["indexPromotionBanners"] = indexPromotionBanners
	c.Data["indexGoodsCates"] = indexGoodsCates
}
